var api = require("../../../../api.js"),
    utils = require("../../../../utils/utils.js"),
    app = getApp();

var countdown = 60;
var settime = function (that) {
    if (countdown == 0) {
        that.setData({
            is_show: true
        })
        countdown = 60;
        return;
    } else {
        that.setData({
            is_show: false,
            last_time: countdown
        })
        countdown--;
    }
    setTimeout(function () {
        settime(that)
    }, 1000)
};


Page({

    /**
     * 页面的初始数据
     */
    data: {
        yzm:'',
        phone:'',
        last_time: '',
        is_show: true
    },
    // 获取手机验证码
    have_yzm:function(){
        var that = this;
        var phone = that.data.phone;
        var code = "";
        for (var i = 0; i < 4; i++) {
            code += Math.floor(Math.random() * 10)
        };
        that.setData({
            code: code
        })
        console.log(code + "========");


        app.request({
            url:api.form.send,
            data: {
                phone: phone,
                code: code
            },
            header: {
                'content-type': "application/x-www-form-urlencoded"
            },
            method: 'POST',
            success: function (res) {
                // 把code存入data判斷
                that.setData({
                    code: code
                })

                
                wx.showToast({
                    title: '發送成功',
                    icon: 'success',
                    image: '',
                    duration: 1500,
                    mask: true,
                })
            },
            fail: function (res) { },
            complete: function (res) { },
        })
        // 将获取验证码按钮隐藏60s，60s后再次显示
        that.setData({
            is_show: (!that.data.is_show)  //false
        })
        settime(that)

    },
    //   提交表单
    formSubmit: function(e) {
        var that = this;
        var code = that.data.code;
        var phone = that.data.phone;
        var yzm = that.data.yzm;
        // if (yzm == '' || yzm == ' ' || code != yzm || phone == '' || phone == ' ' || phone.length == 11) {
        if (code != yzm ) {
            console.log("部位空");
            wx.showToast({
                title: '输入有误',
                icon: 'loading',
                duration: 1500,
                mask: true,
            })
        }
        if (code === yzm & yzm != '') {
            wx.redirectTo({
                url: '/pages/index/index',
            })
            wx.setStorage({
                key: 'user',
                data: {
                    user_code:0
                },
                success: function(res) {},
                fail: function(res) {},
                complete: function(res) {},
            })
        }
    },
    // 手机号
    user_tel: function(e) {
        var that = this;
        that.setData({
            phone: e.detail.value
        })
    },
    // 验证码
    user_yzm: function(e) {
        var that = this;
        that.setData({
            yzm: e.detail.value
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})